//
//  ShopSuggView.m
//  RentTable
//
//  Created by xchuan xchuan on 2024/12/1.
//

#import "ShopSuggView.h"
#import "HotTableCell.h"

@interface ShopSuggView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray<RentModel *> *products;

@end

@implementation ShopSuggView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self loadProductData];
        [self setUI];
    }
    return self;
}

- (void)loadProductData {
    RentManager *rentManager = [RentManager sharedManager];
    NSMutableArray<RentModel *> *rentModels = [rentManager getAllRentData];
    self.products = rentModels;
    
}

- (void)setUI {
    UIView *recomdView = [UIView new];
    recomdView.backgroundColor = myRGBHex(0xFFFFFF);
    recomdView.frame = CGRectMake(16, 0, spDeviceWidth- 32, 450 * scaleH);
    [self addSubview:recomdView];
    
    [recomdView setCornerRadius:20 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight borderColor:myRGBHex(0xFFFFFF) borderWidth:1];
    
    UIImageView *recomdImageView = [UIImageView new];
    recomdImageView.image = [UIImage imageNamed:@"为你推荐"];
    [recomdView addSubview:recomdImageView];
    [recomdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(150, 34 * scaleH));
    }];
    
    [self setupCollectionView:recomdView];
}

- (void)setupCollectionView:(UIView *)hotBackView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((spDeviceWidth - 76) / 2.0, 175 * scaleH);
    layout.sectionInset = UIEdgeInsetsMake(10, 16, 10, 16);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[HotTableCell class] forCellWithReuseIdentifier:@"HotTableCell"];
    
    [hotBackView addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(47 * scaleH);
        make.leading.trailing.bottom.mas_equalTo(0);
    }];
}

#pragma mark - UICollectionViewDelegate & DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HotTableCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotTableCell" forIndexPath:indexPath];
    
    RentModel *product = self.products[indexPath.item];
    
    [cell setPrice:[NSString stringWithFormat:@"%.1f", product.rentPrice] describe:product.rentTitle image:product.rentImage];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected Item at %ld", indexPath.item);
    RentModel *product = self.products[indexPath.item];
    if (self.orderSelect) {
        self.orderSelect(product);
    }
}


@end
